//
//  CATransitionViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/26.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CATransitionViewController.h"

@interface CATransitionViewController ()

@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) NSMutableArray *colorArray;

@property (nonatomic, strong) UIImageView *imageV;

@property (nonatomic, strong) CALayer *rectLayer;

@property (nonatomic, assign) NSInteger colorIndex;

@end

@implementation CATransitionViewController

/**
 *  CATransision可以对图层任何变化平滑过渡的事实使得它成为那些不好做动画的属性图层行为的理想候选。苹果当然意识到了这点，并且当设置了CALayer的content属性的时候，CATransition的确是默认的行为。但是对于视图关联的图层，或者是其他隐式动画的行为，这个特性依然是被禁用的，但是对于你自己创建的图层，这意味着对图层contents图片做的改动都会自动附上淡入淡出的动画。
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
#pragma mark - UIView的过渡
    self.imageArray = [NSMutableArray array];
    [self.imageArray addObject:[self createImageWithColor:[UIColor redColor]]];
    [self.imageArray addObject:[self createImageWithColor:[UIColor blueColor]]];
    [self.imageArray addObject:[self createImageWithColor:[UIColor yellowColor]]];
    [self.imageArray addObject:[self createImageWithColor:[UIColor cyanColor]]];
    
    
//    NSLog(@"%@", self.imageArray);
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.imageV.layer.shadowColor = [UIColor magentaColor].CGColor;
    self.imageV.layer.shadowOpacity = 1;
    self.imageV.layer.shadowOffset = CGSizeMake(0, 0);
//    self.imageV.image = self.imageArray[0];
    self.imageV.backgroundColor = [UIColor redColor];
    [self.view addSubview:_imageV];
    
    
#pragma mark - CALayer的过渡
    self.colorArray = [NSMutableArray array];
    [self.colorArray addObject:[UIColor redColor]];
    [self.colorArray addObject:[UIColor blueColor]];
    [self.colorArray addObject:[UIColor yellowColor]];
    [self.colorArray addObject:[UIColor cyanColor]];
    
    self.rectLayer = [CALayer layer];
    self.rectLayer.frame = CGRectMake(100, 250, 100, 100);
  //  self.rectLayer.backgroundColor = [self.colorArray[0] CGColor];
    self.rectLayer.contents = (__bridge id _Nullable)([self.imageArray[0] CGImage]);
    self.rectLayer.shadowColor = [UIColor greenColor].CGColor;
    self.rectLayer.shadowOffset = CGSizeMake(0, 0);
    self.rectLayer.shadowOpacity = 1;
    [self.view.layer addSublayer:_rectLayer];
    
    
    
    self.colorIndex = 0;
    
    UIButton *transitionButtonOfView = [UIButton buttonWithType:UIButtonTypeCustom];
    transitionButtonOfView.frame = CGRectMake(50, 400, 300, 30);
    [transitionButtonOfView setTitle:@"UIView的CATransition过渡" forState:UIControlStateNormal];
    [transitionButtonOfView setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [transitionButtonOfView addTarget:self action:@selector(UIViewTransition:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:transitionButtonOfView];
    
    UIButton *transitionButtonOfLayer = [UIButton buttonWithType:UIButtonTypeCustom];
    transitionButtonOfLayer.frame = CGRectMake(50, 450, 300, 30);
    [transitionButtonOfLayer setTitle:@"CALayer的CATransition过渡" forState:UIControlStateNormal];
    [transitionButtonOfLayer setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [transitionButtonOfLayer addTarget:self action:@selector(CALayertransition:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:transitionButtonOfLayer];
    
    UIButton *uiviewTransitionBlock = [UIButton buttonWithType:UIButtonTypeCustom];
    uiviewTransitionBlock.frame = CGRectMake(50, 500, 300, 30);
    [uiviewTransitionBlock setTitle:@"CATransition的Block过渡" forState:UIControlStateNormal];
    [uiviewTransitionBlock setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [uiviewTransitionBlock addTarget:self action:@selector(transitionBlock:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:uiviewTransitionBlock];
    
    UIButton *customTransitionAnimation = [UIButton buttonWithType:UIButtonTypeCustom];
    customTransitionAnimation.frame = CGRectMake(50, 550, 300, 30);
    [customTransitionAnimation setTitle:@"自定义过渡动画" forState:UIControlStateNormal];
    [customTransitionAnimation setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [customTransitionAnimation addTarget:self action:@selector(customTransitionAnimation:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:customTransitionAnimation];
}

- (UIImage *)createImageWithColor:(UIColor *)color
{
    UIGraphicsBeginImageContext(CGSizeMake(100, 100));
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddRect(ctx, CGRectMake(0, 0, 100, 100));
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    CGContextFillPath(ctx);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    return image;
}

// UIView的CATransition过渡
- (void)UIViewTransition:(UIButton *)button
{
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0;
    transition.subtype = kCATransitionFromLeft;
    transition.type = kCATransitionPush;
 //   [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; // 可以设置动画的速度方式
    [self.imageV.layer addAnimation:transition forKey:nil];
    
// 方法一
//    UIImage *currentImage = self.imageV.image;
//    NSUInteger index = [self.imageArray indexOfObject:currentImage];
//    index = (index + 1) % [self.imageArray count];
//    self.imageV.image = self.imageArray[index];
    
    // 方法二
    if (self.colorIndex == 3) {
        self.colorIndex = 0;
    }
    self.colorIndex++;
    
    self.imageV.backgroundColor = self.colorArray[self.colorIndex];
 //   self.imageV.backgroundColor = [UIColor redColor]; // 这个测试说明只要layer层属性重新复制，CATransition就会执行
    // 结论这两种方法都可以改变image关联图层的展现形式，这说明CATransition是添加在layer层的动画，只是在改变layer状态的时候添加一个过渡效果而已(其实UIView的任何操作，系统都会转化为layer层的展现，因此UIView状态改变就是layer层的改变)
}

// CALayer的CATransition过渡
- (void)CALayertransition:(UIButton *)button
{
    // 除了给layer添加CATransition动画外，我们也可以为layer的actions添加要改变的状态，效果是一样的
    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"蒙版"]];
    CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
    [filter setDefaults];
    [filter setValue:ciImage forKey:kCIInputImageKey];
    [filter setValue:@(10) forKey:kCIInputIntensityKey];
    
    
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0;
    transition.subtype = kCATransitionFromLeft;
    transition.type = kCATransitionPush;
   // transition.filter = filter; // filter实际上没有任何作用
   // [self.rectLayer addAnimation:transition forKey:nil];
    
    if (self.colorIndex == 3) {
        self.colorIndex = 0;
    }
    self.colorIndex++;
// 方法一
//    self.rectLayer.actions = @{@"backgroundColor":transition};
//    self.rectLayer.backgroundColor = [self.colorArray[self.colorIndex] CGColor];
 
    // 方法二
    self.rectLayer.actions = @{@"contents":transition};
    self.rectLayer.contents = (__bridge id _Nullable)([self.imageArray[self.colorIndex] CGImage]);

}

// UIView的CATranstion的Block过渡
- (void)transitionBlock:(UIButton *)button
{
    [UIView transitionWithView:self.imageV duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        UIImage *currentImage = self.imageV.image;
            NSUInteger index = [self.imageArray indexOfObject:currentImage];
            index = (index + 1) % [self.imageArray count];
            self.imageV.image = self.imageArray[index];
    } completion:nil];
}

// 自定义过渡动画
- (void)customTransitionAnimation:(UIButton *)button
{
    // 思路就是先截取当前的视图，然后对截取的图片进行自定义动画，这就是自定义过渡动画
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:ctx];
    UIImage *snapImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *snapImageV = [[UIImageView alloc] initWithImage:snapImage];
    [self.view addSubview:snapImageV];
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    // 进行动画
    [UIView animateWithDuration:1.0 animations:^{
        CGAffineTransform affine = CGAffineTransformMakeScale(0.01, 0.01);
        affine = CGAffineTransformRotate(affine, M_PI);
        snapImageV.transform = affine;
        snapImageV.alpha = 0.0;
    } completion:^(BOOL finished) {
        [snapImageV removeFromSuperview];
    }];
    
}

@end
